{% extends "base_generic.html" %}

{% block title %}{{ book.title }} - Local Library{% endblock %}

{% block content %}
    <div class="row">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h3 class="mb-0">{{ book.title }}</h3>
                    {% if perms.catalog.change_book %}
                        <div class="btn-group">
                            <a href="{% url 'book-update' book.pk %}" class="btn btn-outline-primary">
                                <i class="bi bi-pencil"></i> Edit
                            </a>
                            <a href="{% url 'book-delete' book.pk %}" class="btn btn-outline-danger">
                                <i class="bi bi-trash"></i> Delete
                            </a>
                        </div>
                    {% endif %}
                </div>
                <div class="card-body">
                    <div class="row mb-3">
                        <div class="col-sm-3">
                            <strong>Author:</strong>
                        </div>
                        <div class="col-sm-9">
                            <div class="mb-3">
                                <h6 class="text-muted mb-1">Author(s)</h6>
                                {% for author in book.author.all %}
                                    <a href="{{ author.get_absolute_url }}" class="text-decoration-none me-2">
                                        <span class="badge bg-primary">{{ author.get_full_name }}</span>
                                    </a>
                                {% empty %}
                                    <span class="text-muted">No authors listed</span>
                                {% endfor %}
                            </div>
                        </div>
                    </div>

                    <div class="row mb-3">
                        <div class="col-sm-3">
                            <strong>Summary:</strong>
                        </div>
                        <div class="col-sm-9">
                            {{ book.summary }}
                        </div>
                    </div>

                    <div class="row mb-3">
                        <div class="col-sm-3">
                            <strong>ISBN:</strong>
                        </div>
                        <div class="col-sm-9">
                            {{ book.isbn }}
                        </div>
                    </div>

                    {% if book.publication_date %}
                        <div class="row mb-3">
                            <div class="col-sm-3">
                                <strong>Publication Date:</strong>
                            </div>
                            <div class="col-sm-9">
                                {{ book.publication_date }}
                            </div>
                        </div>
                    {% endif %}

                    {% if book.genre.all %}
                        <div class="row mb-3">
                            <div class="col-sm-3">
                                <strong>Genre:</strong>
                            </div>
                            <div class="col-sm-9">
                                {% for genre in book.genre.all %}
                                    <span class="badge bg-secondary me-1">{{ genre.name }}</span>
                                {% endfor %}
                            </div>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>

        <div class="col-md-4">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0"><i class="bi bi-collection"></i> Available Copies</h5>
                </div>
                <div class="card-body">
                    {% if available_instances %}
                        <p class="mb-3">
                            <span class="badge bg-success">{{ available_instances|length }} Available</span>
                        </p>

                        {% if user.is_authenticated %}
                            {% for instance in available_instances|slice:":3" %}
                                <div class="d-flex justify-content-between align-items-center mb-2 p-2 border rounded">
                                    <div>
                                        <small class="text-muted">Copy ID: {{ instance.id|slice:":8" }}...</small><br>
                                        <small>{{ instance.imprint }}</small>
                                    </div>
                                    <a href="{% url 'borrow-book' instance.id %}" class="btn btn-primary btn-sm">
                                        <i class="bi bi-bookmark-plus"></i> Borrow
                                    </a>
                                </div>
                            {% endfor %}

                            {% if available_instances|length > 3 %}
                                <p class="text-muted small">And {{ available_instances|length|add:"-3" }} more
                                    copies...</p>
                            {% endif %}
                        {% else %}
                            <p class="text-muted">Please <a href="{% url 'login' %}">login</a> to borrow books.</p>
                        {% endif %}
                    {% else %}
                        <div class="text-center py-3">
                            <i class="bi bi-x-circle-fill text-danger"></i>
                            <p class="mb-0 mt-2">No copies available</p>
                            <small class="text-muted">Check back later</small>
                        </div>
                    {% endif %}
                </div>
            </div>

            <!-- All Copies Status -->
            <div class="card mt-3">
                <div class="card-header">
                    <h6 class="mb-0"><i class="bi bi-list-check"></i> All Copies</h6>
                </div>
                <div class="card-body">
                    {% for instance in book.bookinstance_set.all %}
                        <div class="d-flex justify-content-between align-items-center mb-2 p-2 border rounded">
                            <div>
                                <small class="text-muted">{{ instance.id|slice:":8" }}...</small><br>
                                <small>{{ instance.imprint }}</small>
                            </div>
                            <div class="text-end">
                                {% if instance.status == 'a' %}
                                    <span class="badge bg-success">Available</span>
                                {% elif instance.status == 'o' %}
                                    <span class="badge bg-warning">On Loan</span>
                                    {% if instance.due_back %}
                                        <br><small class="text-muted">Due: {{ instance.due_back }}</small>
                                    {% endif %}
                                {% elif instance.status == 'm' %}
                                    <span class="badge bg-secondary">Maintenance</span>
                                {% elif instance.status == 'r' %}
                                    <span class="badge bg-info">Reserved</span>
                                {% endif %}
                            </div>
                        </div>
                    {% empty %}
                        <p class="text-muted">No copies available</p>
                    {% endfor %}
                </div>
            </div>
        </div>
    </div>

    <div class="mt-4">
        <a href="{% url 'books' %}" class="btn btn-outline-secondary">
            <i class="bi bi-arrow-left"></i> Back to Books
        </a>
    </div>
{% endblock %} 